<template>
  <el-container class="home-container">
    <!-- 头部 -->
    <el-header>
      <el-tooltip class="item" effect="dark" content="点击返回欢迎页" placement="right-end">
        <router-link to="/welcome" class="welcomeLink" >后台管理系统</router-link>
      </el-tooltip>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <!-- 左侧 -->
      <el-aside :width="isCollapse?'60px':'200px'">
        <!-- 侧边栏折叠按钮 -->
        <div class="toggle-button" @click="toggleCollapse">{{zhedie}}</div>
          <!-- 侧边栏菜单 -->
        <el-menu active-text-color="skyblue" 
                :unique-opened="true" 
                :collapse="isCollapse"
                :collapse-transition="false"
                router
                :default-active="activePath"
        >
            <!-- 一级菜单 -->
            <el-submenu v-for="item in menulist" :key="item.id" :index="'/'+item.path">
                <template slot="title">
                <i :class="iconsObj[item.id]"></i>
                <span>{{item.authName}}</span>
                </template>
                <!-- 二级菜单 -->
                <el-menu-item v-for="subItem in item.children" 
                              :key="subItem.id" 
                              :index="'/'+subItem.path"
                              @click="saveNavState('/'+subItem.path)"
                >
                    <template slot="title">
                    <i class="el-icon-menu"></i>
                    <span>{{subItem.authName}}</span>
                    </template> 
                </el-menu-item>
            </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
    created() {
        this.getMenuList()
        this.activePath=window.sessionStorage.getItem('activePath')
    },
    data() {
        return {
            menulist:[],
            iconsObj:{
              '125':'iconfont icon-addressbook_fill',
              '103':'iconfont icon-activity',
              '101':'iconfont icon-accessory',
              '102':'iconfont icon-add',
              '145':'iconfont icon-addition',
            },
            isCollapse:false ,
            zhedie:'折叠',
            activePath:''
        }
    },
    methods: {
    logout() {
      window.sessionStorage.clear();
      this.$message.success("退出成功！");
      this.$router.push("/login");
    },
    async getMenuList(){
        const {data:res} = await this.$http.get('menus')
        if(res.meta.status !== 200) return this.$message.error(res.meta.msg)
        this.menulist=res.data
        console.log(res)
    },
    toggleCollapse(){
      this.isCollapse = !this.isCollapse
      console.log(this.isCollapse);
    },
    saveNavState(activePath){
      window.sessionStorage.setItem('activePath',activePath)
      this.activePath = activePath
    }
  },
 
};
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  padding: 0 50px;
  background-color: skyblue;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 24px;
  color: aliceblue;
  font-weight: 700;
  span {
    margin-left: 800px;
  }
}
.el-aside {
  background-color: #fff
}
.el-main {
  background-color: gainsboro;
}
.iconfont{
  margin-right: 10px;
}
.toggle-button{
  display: flex;
  justify-content: center;
  cursor: pointer;
  letter-spacing: 0.2em;
  color: #000;
}
.welcomeLink{
  text-decoration: none;
  margin: 0 auto;
  color: #fff;
}
.welcomeLink:hover{
  color: pink;
}
</style>